/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl.utils;

import java.io.File;
import java.io.FileNotFoundException;

import org.dctmcommons.ISessionCallback;
import org.dctmcommons.ISessionHelper;
import org.dctmcommons.impl.SessionHelper;

import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSysObject;
import com.documentum.fc.common.DfException;

/**
 * Updates content for an existing object.
 * @author Mark Davidson
 */
public class ContentUpdater {

	/** The usage message. */
	private static final String MSG_USAGE =
		"Usage: ContentUpdater <repository> <username> <password> " +
		"<source> <qualification>";
	
	/** The session helper. */
	private ISessionHelper sessionHelper = null;

	/** The repository name. */
	private String repository = null;
	
	/** The username. */
	private String username = null;
	
	/** The password. */
	private String password = null;
	
	/** The domain. */
	private String domain = null;
	
	/**
	 * Default constructor.
	 * @param repository The repository
	 * @param username The username
	 * @param password The password
	 */
	public ContentUpdater(final String repository,
						  final String username,
						  final String password) {
		this.repository = repository;
		this.username = username;
		this.password = password;
	}
	
	/**
	 * Entry point to content update functionality.
	 * @param source The path to the content file
	 * @param qualification The DQL qualification string that identifies the
	 * object to be updated.
	 */
	public void execute(final String source,
						final String qualification) throws Exception {
		sessionHelper = SessionHelper.getInstance();
		sessionHelper.useSession(repository,
								 username,
								 password,
								 domain,
								 true,
								 new ISessionCallback() {
			@Override
			public void useSession(final IDfSession session)
				throws DfException {
				try {
					updateContent(session, source, qualification);
				} catch (Throwable t) {
					throw new DfException(t);
				}
			}
		});
	}
	
	/**
	 * Updates the content.
	 * @param session The session
	 * @param source The source file
	 * @param qualification The DQL qualification
	 * @throws DfException The repository exception
	 * @throws FileNotFoundException Thrown if the source file can't be found
	 */
	private void updateContent(final IDfSession session,
							   final String source,
							   final String qualification)
		throws DfException, FileNotFoundException {
		final IDfSysObject sysobject =
					(IDfSysObject) session
									.getObjectByQualification(qualification);
		sysobject.checkout();
		updateContent(sysobject, source);
		sysobject.checkin(false, null);
	}
	
	/**
	 * Sets the content of the sysobject.
	 * @param sysobject The sysobject
	 * @param source The path to the source file
	 * @throws DfException The repository exception
	 * @throws FileNotFoundException Thrown if the source file can't be found
	 */
	private void updateContent(final IDfSysObject sysobject,
							   final String source)
		throws DfException, FileNotFoundException {
		final File file = new File(source);
		if (file.exists() && file.isFile()) {
			sysobject.setFile(file.getAbsolutePath());
		} else {
			throw new FileNotFoundException();
		}
	}
	
	/**
	 * @param args The command line arguments
	 */
	public static void main(final String[] args) {
		if (args != null && args.length >= 4) {
			try {
				System.out.println("Updating content..");
				new ContentUpdater(args[0], args[1], args[2])
						.execute(args[3], args[4]);
				System.out.println("Finished updating content");
			} catch (Throwable t) {
				System.out.println("Content updated failed. " + t.getMessage());
				t.printStackTrace();
			}
		} else {
			System.out.println(MSG_USAGE);
		}
	}
}
